package com.weiyuwqj.admin.controller.admin.account;

import com.weiyuwqj.admin.controller.admin.log.annotation.LogAnnotation;
import com.weiyuwqj.api.service.BusAccountLogService;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.model.convert.AccountRechargeAddtimeConvert;
import com.weiyuwqj.api.model.convert.ModelConvert;
import com.weiyuwqj.api.model.vo.BusAccountLogVo;
import com.weiyuwqj.api.util.ExcelUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * The function of saving, modifying and deleting the site module
 *
 * Copyright 2015-2019 Yunfucloud technology co., Ltd.
 * Created by Jiaqi on 15/11/17.
 */
@Controller
@RequestMapping("/admin/accountlog")
public class AccountLogController {

    @Autowired
    BusAccountLogService busAccountLogService;


    /**
     * 列出所有日志记录
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/list", method=RequestMethod.POST)
    public  Object list(
            @ModelAttribute Paginator p,
            @RequestParam (defaultValue = "") String userId,
            @RequestParam (defaultValue = "") String commonCond,
            @RequestParam (defaultValue = "") String beginDateTime,
            @RequestParam (defaultValue = "") String endDateTime,
            @RequestParam (defaultValue ="") String type,
            String orderName,
            String orderWay){
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("userId",userId);
        map.put("commonCond",commonCond);
        if(StringUtils.isNotEmpty(beginDateTime)){
            map.put("beginDateTime", beginDateTime);
        }
        if(StringUtils.isNotEmpty(endDateTime)){
            map.put("endDateTime", endDateTime);
        }
        if (StringUtils.isNotEmpty(orderName)){
            map.put("orderName", orderName);
        }
        if (StringUtils.isNotEmpty(orderWay)){
            map.put("orderWay", orderWay);
        }
        map.put("type",type);

        return  busAccountLogService.getList(p, map);
    }

    /**
     * 资金日志导出
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/excel/fundstlog")
    @LogAnnotation(module ="账户管理/资金日志管理",description = "导出资金日志信息")
    public Object fundstlogExcel(HttpServletRequest request,String userId,String type,String commonCond,String beginDateTime,String endDateTime){

        String[] cArray={"ID","用户名","真实姓名","操作类型","操作金额","总金额","可用金额","冻结金额","待收金额","待还金额","对方交易","添加时间"};
        String[] eArray={"Id","Username","Realname","DictType","OperMoney","Total","Usable","Frozen","Wait","Repay","TradeUserId","Addtime"};
        ModelConvert[] converts={null,null,null,null,null,null,null,null,null,
                null,null,new AccountRechargeAddtimeConvert()};

        List<String> cList= Arrays.asList(cArray);
        List<String> eList=Arrays.asList(eArray);
        List<ModelConvert> convertList=Arrays.asList(converts);
        Map map=new HashMap();
        map.put("userId",userId);
        map.put("type",type);
        map.put("commonCond",commonCond);
        map.put("beginDateTime", beginDateTime);
        map.put("endDateTime", endDateTime);

        List<BusAccountLogVo> contentList = busAccountLogService.getList(map);
        String serverDir=request.getServletContext().getRealPath("/")+"admin/export/";
        String excelFilePath=ExcelUtils.export("资金日志",cList,eList,convertList,contentList,serverDir);
        HashMap ret= Message.OK;
        ret.put("file","export/"+excelFilePath);
        return ret;
    }
}